// 测试余额加载和更新功能
const testBalanceFunction = () => {
  console.log('=== 测试余额功能 ===')
  
  // 模拟后端返回的余额数据
  const mockBalanceResponse = {
    code: 200,
    message: "操作成功",
    data: {
      balance: 150.50,
      cardNumber: "39000909000000",
      lastUpdateTime: "2024-01-01 10:00:00"
    }
  }
  
  console.log('模拟后端余额响应:', mockBalanceResponse)
  
  // 模拟前端处理逻辑
  const processBalanceData = (res) => {
    if (res.statusCode === 200 && res.data) {
      let balanceData = res.data
      
      // 处理数据格式
      if (typeof balanceData === 'string') {
        try {
          balanceData = JSON.parse(balanceData)
        } catch (e) {
          console.error('余额数据解析失败:', e)
          return null
        }
      }
      
      // 检查是否为Result格式的响应
      if (balanceData.code !== undefined) {
        if (balanceData.code !== 200) {
          console.error('获取余额失败:', balanceData.message)
          return null
        }
        balanceData = balanceData.data
      }
      
      // 更新余额
      const balance = balanceData.balance || balanceData.amount || 0
      const formattedBalance = balance.toFixed(2)
      
      console.log('✅ 余额处理成功')
      console.log('原始余额:', balance)
      console.log('格式化余额:', formattedBalance)
      console.log('卡号:', balanceData.cardNumber)
      console.log('更新时间:', balanceData.lastUpdateTime)
      
      return {
        cardBalance: formattedBalance,
        cardNumber: balanceData.cardNumber,
        lastUpdateTime: balanceData.lastUpdateTime
      }
    }
    return null
  }
  
  // 模拟HTTP响应
  const mockResponse = {
    statusCode: 200,
    data: mockBalanceResponse
  }
  
  // 执行测试
  const result = processBalanceData(mockResponse)
  
  if (result) {
    console.log('✅ 余额功能测试成功！')
    console.log('最终余额数据:', result)
  } else {
    console.log('❌ 余额功能测试失败')
  }
  
  // 测试不同余额值
  const testCases = [
    { balance: 0, expected: '0.00' },
    { balance: 100.5, expected: '100.50' },
    { balance: 999.99, expected: '999.99' },
    { balance: 1500.123, expected: '1500.12' }
  ]
  
  console.log('\n=== 测试不同余额值格式化 ===')
  testCases.forEach((testCase, index) => {
    const formatted = testCase.balance.toFixed(2)
    const passed = formatted === testCase.expected
    console.log(`测试 ${index + 1}: ${testCase.balance} -> ${formatted} ${passed ? '✅' : '❌'}`)
  })
}

// 运行测试
testBalanceFunction() 